<?php
namespace app\logic;

use app\common\MyExcel;
use app\common\MyFile;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ExcelLogic
{
    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    public function dealExcel()
    {
        $data = (new MyFile())->getFileToJsonData('E:\gao\tp6\tp\app\common\data.json');
        $res = $this->getRes($data);
        return (new MyExcel())->exportTemplateExcel($res);
    }

    /**
     * @param $data
     * @return array
     */
    public function getRes($data): array
    {
        $res = [];
        foreach ($data as $item) {
            if (isset($item['report'])) {
                $channel = (isset($item['channel'])) ? $item['channel'] : "其他";
                $trade = (isset($item['trade'])) ? $item['trade'] : "其他";
                $career = (isset($item['career'])) ? $item['career'] : "其他";
                $month = (isset($item['month'])) ? $item['month'] : "";
                if (empty($res[$channel])) {
                    $res[$channel][$trade] = [];
                }
                if (empty($res[$channel][$trade])) {
                    $res[$channel][$trade][$career] = [];
                }
                $res[$channel][$trade][$career][] = [
                    "report" => $item['report'],
                    "month" => $month
                ];

            }
        }
        return $res;
    }



}
